/*
 *  ---------
 * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
 * |#       #|  
 * |#       #|  Copyright (c) 1999-2006 CardContact Software & System Consulting
 * |'##> <##'|  Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de)
 *  --------- 
 *
 *  This file is part of OpenSCDP.
 *
 *  OpenSCDP is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  OpenSCDP is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSCDP; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package com.jl.process.util;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

import com.jl.js.core.ByteArray;

/**
 * Utility class to check arguments passed to host objects in ECMA script
 * @author Andreas Schwier (info@cardcontact.de)
 */
public class ArgChecker {

    /**
     * Check that argument list is within range. Throw GPError.INVALID_ARGUMENTS if number does not match
     * 
     * @param thisObj Object for which the method is called
     * @param clazzName Name of class
     * @param args Arguments array
     * @param min Minimum number of arguments
     * @param max Maximum number of arguments
     * 
     */
    public static void checkRange(Scriptable thisObj, String clazzName, Object[] args, int min, int max) {
        if (args.length < min) {
            GPError.throwAsGPErrorEx(thisObj, clazzName, GPError.ARGUMENTS_MISSING, args.length, "Too few arguments");
        }
        if (args.length > max) {
            GPError.throwAsGPErrorEx(thisObj, clazzName, GPError.INVALID_ARGUMENTS, args.length, "Too many arguments");
        }
    }

    
    
    /**
     * Get integer argument or default value if argument is not defined
     * Throw GPError.INVALID_TYPE if parameter is not of type number
     * 
     * @param thisObj Object for which the method is called
     * @param clazzName Name of class
     * @param args Argument array
     * @param index Number of argument to operate on
     * @param defaultValue Default value, if argument not passed
     * 
     * @return Argument value or default
     */
    public static int getInt(Scriptable thisObj, String clazzName, Object[] args, int index, int defaultValue) {
        if (args.length > index) {
            if (args[index] instanceof Number) {
                return ((Number)args[index]).intValue();
            } else {
                throw GPError.throwAsGPErrorEx(thisObj, clazzName, GPError.INVALID_TYPE, index, "Expected number argument");
            }
        } else {
            return defaultValue;
        }
    }
    

    
    /**
     * Get long integer argument or default value if argument is not defined
     * Throw GPError.INVALID_TYPE if parameter is not of type number
     * 
     * @param thisObj Object for which the method is called
     * @param clazzName Name of class
     * @param args Argument array
     * @param index Number of argument to operate on
     * @param defaultValue Default value, if argument not passed
     * 
     * @return Argument value or default
     */
    public static long getLong(Scriptable thisObj, String clazzName, Object[] args, int index, long defaultValue) {
        if (args.length > index) {
            if (args[index] instanceof Number) {
                return ((Number)args[index]).longValue();
            } else {
                throw GPError.throwAsGPErrorEx(thisObj, clazzName, GPError.INVALID_TYPE, index, "Expected number argument");
            }
        } else {
            return defaultValue;
        }
    }
    

    
    /**
     * Get byte argument or default value if argument is not defined
     * Throw GPError.INVALID_TYPE if parameter is not of type number
     * 
     * @param thisObj Object for which the method is called
     * @param clazzName Name of class
     * @param args Argument array
     * @param index Number of argument to operate on
     * @param defaultValue Default value, if argument not passed
     * 
     * @return Argument value or default
     */
    public static byte getByte(Scriptable thisObj, String clazzName, Object[] args, int index, byte defaultValue) {
        if (args.length > index) {
            if (args[index] instanceof Number) {
                return ((Number)args[index]).byteValue();
            } else {
                throw GPError.throwAsGPErrorEx(thisObj, clazzName, GPError.INVALID_TYPE, index, "Expected number argument");
            }
        } else {
            return defaultValue;
        }
    }
    
    
    
    /**
     * Get string argument or default value if argument is not defined
     * Throw GPError.INVALID_TYPE if parameter is not of type String
     * 
     * @param thisObj Object for which the method is called
     * @param clazzName Name of class
     * @param args Argument array
     * @param index Number of argument to operate on
     * @param defaultValue Default value, if argument not passed
     * 
     * @return Argument value or default
     */
    public static String getString(Scriptable thisObj, String clazzName, Object[] args, int index, String defaultValue) {
        if (args.length > index) {
            if (args[index] instanceof String) {
                return Context.toString(args[index]);
            } else if (args[index] == Context.getUndefinedValue()) {
                return defaultValue;
            } else {
                throw GPError.throwAsGPErrorEx(thisObj, clazzName, GPError.INVALID_TYPE, index, "Expected string argument");
            }
        } else {
            return defaultValue;
        }
    }
    
    
    
    /**
     * Get boolean argument or default value if argument is not defined
     * Throw GPError.INVALID_TYPE if parameter is not of type number
     * 
     * @param thisObj Object for which the method is called
     * @param clazzName Name of class
     * @param args Argument array
     * @param index Number of argument to operate on
     * @param defaultValue Default value, if argument not passed
     * 
     * @return Argument value or default
     */
    public static boolean getBoolean(Scriptable thisObj, String clazzName, Object[] args, int index, boolean defaultValue) {
        if (args.length > index) {
            if (args[index] instanceof Boolean) {
                return ((Boolean)args[index]).booleanValue();
            } else {
                throw GPError.throwAsGPErrorEx(thisObj, clazzName, GPError.INVALID_TYPE, index, "Expected number argument");
            }
        } else {
            return defaultValue;
        }
    }
    
    
    
    /**
     * Get ByteString argument or default value if argument is not defined
     * Throw GPError.INVALID_TYPE if parameter is not of type ByteString
     * 
     * @param thisObj Object for which the method is called
     * @param clazzName Name of class
     * @param args Argument array
     * @param index Number of argument to operate on
     * @param defaultValue Default value, if argument not passed
     * 
     * @return Argument value or default
     */
    public static ByteArray getByteString(Scriptable thisObj, String clazzName, Object[] args, int index, ByteArray defaultValue) {
        if (args.length > index) {
            if (args[index] == null) {
                return null;
            }
            if (args[index] instanceof ByteArray) {
                return ((ByteArray)args[index]);
            } else {
                throw GPError.throwAsGPErrorEx(thisObj, clazzName, GPError.INVALID_TYPE, index, "Expected ByteString argument");
            }
        } else {
            return defaultValue;
        }
    }
}
